package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.RoomTypeDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 * 房间
 */
@RestController
@RequestMapping("RoomType")
public class RoomTypeController extends BaseController {

    @Autowired
    @Qualifier("RoomTypeServiceImplOne")
    private RoomTypeService roomTypeService;

    @Autowired
    @Qualifier("RoomServiceImplOne")
    private RoomService roomService;

    @Autowired
    @Qualifier("BerthServiceImplOne")
    private BerthService berthService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @Autowired
    @Qualifier("InstitutionServiceImplOne")
    private InstitutionService institutionService;


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            int num = jsonObject.getInteger("num");
            double price = jsonObject.getDouble("price");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/RoomType/Add");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();


            RoomType roomType = roomTypeService.getByNameAndInstitutionCode(name,institutionCode);
            if(roomType == null){
                roomType = new RoomType();
                roomType.setRoomTypeCode(UUID.randomUUID().toString());
                roomType.setName(name);
                roomType.setNum(num);
                roomType.setStatus(0);
//                if(institutionArg.getChargeTime().equals("day")){
//                    roomType.setPrice(price);
//                }
//                if(institutionArg.getChargeTime().equals("month")){
//                    roomType.setPriceMonth(price);
//                }
                roomType.setPriceMonth(price);
                roomType.setInstitutionCode(institutionCode);
                roomTypeService.insert(roomType);
                Object data = new Object[]{};
                return success(data);
            }else {
                return failed("房间类型已存在");
            }
        }catch (Exception e){
            insertError("/RoomType/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult EditByroomTypeCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            int num = jsonObject.getInteger("num");
            int status = jsonObject.getInteger("status");
            double price = jsonObject.getDouble("price");
            String roomTypeCode = jsonObject.getString("roomTypeCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/RoomType/Edit");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();


            RoomType roomType = roomTypeService.getByNameAndInstitutionCode(name,institutionCode);
            if(roomType!=null && !roomType.getRoomTypeCode().equals(roomTypeCode)){
                return failed("此房间类型已存在");
            }else {
                roomType = roomTypeService.getByRoomTypeCode(roomTypeCode);
            }
            roomType.setName(name);
            roomType.setNum(num);
            roomType.setStatus(status);
//            if(institutionArg.getChargeTime().equals("day")){
//                roomType.setPrice(price);
//            }
//            if(institutionArg.getChargeTime().equals("month")){
//                roomType.setPriceMonth(price);
//            }
            roomType.setPriceMonth(price);
            roomTypeService.updateByRoomTypeCode(roomType);

            Object data = new Object[]{};
            return success(data);

        }catch (Exception e){
            insertError("/RoomType/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    /**
     * 查询所有状态为1的房间类型
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult GetListAll(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/RoomType/List");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            List<RoomType> listRoomType = roomTypeService.getListAllByInstitutionCode(institutionCode);
            return success(listRoomType);
        }catch (Exception e){
            insertError("/RoomType/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/RoomType/Page");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<RoomType> pageInfoRoomType = roomTypeService.getPageList(pageIndex,pageSize,institutionCode,name,status);
            return success(pageInfoRoomType);
        }catch (Exception e){
            insertError("/RoomType/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //gov
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/RoomType/Page");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<RoomType> pageInfoRoomType = roomTypeService.pageByGovCode(pageIndex,pageSize,staff.getGovernmentCode());

            List<RoomTypeDTO> listDTO = new ArrayList<>();
            for(RoomType roomType:pageInfoRoomType.getList()){
                RoomTypeDTO roomTypeDTO = mapper.map(roomType,RoomTypeDTO.class);

                Institution institution = institutionService.getByInstitutionCode(roomType.getInstitutionCode());
                User user = userService.getByUserCode(institution.getUserCode());
                roomTypeDTO.setInstitutionName(user.getName());

                listDTO.add(roomTypeDTO);

            }

            pageInfoRoomType.setList(Collections.unmodifiableList(listDTO));

            return success(pageInfoRoomType);
        }catch (Exception e){
            insertError("/RoomType/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    /**
     * 根据房间code查询房间价格
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "GetPriceByBerthCode", method = RequestMethod.POST)
    public MyResult GetPriceByBerthCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String berthCode = jsonObject.getString("berthCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/RoomType/GetPriceByBerthCode");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }
            Berth berth = berthService.getByBerthCode(berthCode);
            if(berth==null){
                return failed("床位不存在");
            }
            Room room = roomService.getByRoomCode(berth.getRoomCode());
            if(room==null){
                return failed("房间不存在");
            }
            RoomType roomType = roomTypeService.getByRoomTypeCode(room.getRoomTypeCode());
            if(roomType==null){
                return failed("房间类型不存在");
            }else {
                return success(roomType);
            }

        }catch (Exception e){
            insertError("/RoomType/GetPriceByBerthCode", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


}
